package models;

import models.constants.DeletedStatus;
import play.db.jpa.JPA;
import product.ProductSpec;
import util.common.UStringUtil;

import javax.persistence.*;
import java.util.List;

/**
 * Created by liming on 16/7/16.
 * 采购计划明细
 */
@Entity
@Table(name = "admin_stock_in_plan_item")
public class AdminStockInPlanItem extends BaseModel {

    /**
     * 采购计划
     */
    @ManyToOne
    @JoinColumn(name = "admin_stock_plan")
    public AdminStockInPlan adminStockInPlan;

    /**
     * 规格产品
     */
    @ManyToOne
    @JoinColumn(name = "product_spec_id")
    public ProductSpec productSpec;

    /**
     * 计划采购单价
     */
    @Column(name = "plan_price")
    public Double planPrice;

    /**
     * 计划采购数量
     */
    @Column(name = "plan_num")
    public Double planNum;

    /**
     * 计划采购金额
     */
    @Column(name = "plan_amount")
    public Double planAmount;

    /**
     * 计划时库存数量
     */
    @Column(name = "sys_num")
    public Double sysNum;

    /**
     * 需求数量
     */
    @Column(name = "needs_num")
    public Double needsNum;

    /**
     * 需求数量 - 计划时库存数量
     */
    @Column(name = "diffNum")
    public Double diffNum;

    /**
     * 预估采购数量
     */
    @Column(name = "estimateNum")
    public Double estimateNum;

    /**
     * 日均销量
     */
    @Column(name = "dms")
    public Double dms;

    /**
     * 标记
     */
    @Column(name = "color_mark")
    public String colorMark;

    /**
     * 持续天数
     */
    @Column(name = "lasting_days")
    public Double lastingDays;

    /**
     * 备注
     */
    @Column(name = "remark" , length = 1000)
    public String remark;


    /**
     * 获取采购计划的产品列表
     */
    public static List<AdminStockInPlanItem> loadStockInPlanItemList(long id) {
        return AdminStockInPlanItem.find("adminStockInPlan.id=? and adminStockInPlan.deletedStatus=? order by productSpec.dCode", id, DeletedStatus.UN_DELETED).fetch();
    }

    /**
     * 检查未生效的采购计划单,一种产品不可重复
     */
    public static String checkProSpec(List<Integer> insertIdList) {
        String str = "未生效的采购计划中:";
        String inserIds = UStringUtil.concatStr(",", insertIdList);

        List<AdminStockInPlanItem> stockInPlanItemList = AdminStockInPlanItem.find(" adminStockInPlan.auditUser = null and adminStockInPlan.deletedStatus=? and productSpec.id in (" + inserIds + ")", DeletedStatus.UN_DELETED).fetch();
        if (stockInPlanItemList != null) {
            for (AdminStockInPlanItem item : stockInPlanItemList) {
                str += item.productSpec.product.name + item.productSpec.spec;
            }
        }
        return str;
    }

    /**
     * 删除
     * param id
     */
    public static void deleteItem(Integer[] delItemIdArray) {
        String delIdStr = UStringUtil.concatStr(",", delItemIdArray);
        AdminStockInPlanItem.delete("id in (" + delIdStr + ")");
    }

    public static void mark(Integer[] ids,String color){
        String str=UStringUtil.concatStr(",",ids);
        String sqlUpdate = "update admin_stock_in_plan_item a set a.color_mark = '"+color+"'  where a.id in ("+str+")";
        JPA.em().createNativeQuery(sqlUpdate).executeUpdate();
    }

}
